﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using JXT.PrimaryKey.Batman.Core.Language;
using JXT.PrimaryKey.Batman.Web.Mvc.Controllers;

namespace JXT.PrimaryKey.Batman.Web.Mvc.Models.Metadata
{
    public class BooleanRequired : RequiredAttribute, IClientValidatable
    {
        public override bool IsValid(object value)
        {
            if (value is Boolean)
            {
                return (bool)value == true;
            }
            return false;
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            string errorMessage;
            if (String.IsNullOrEmpty(ErrorMessageResourceName))
            {
                errorMessage = ErrorMessage;
            }
            else
            {
                errorMessage = (context.Controller as BaseController).Context.String(ErrorMessageResourceName);
            }
            return new ModelClientValidationRule[] { 
                new ModelClientValidationRule() { ValidationType = "booleanrequired", ErrorMessage = errorMessage } 
            };
        }
    }
}
